package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.CustomerPlan;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.CustomerPlanQueryObject;
import cn.wolfcode.crm.service.ICustomerPlanService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("customerPlan")
public class CustomePlanController {

    @Autowired
    private ICustomerPlanService customerPlanService;

    @RequestMapping("potential")
    @RequiresPermissions("customerPlan:potential")
    @PermissionName("潜在客户开发计划")
    public String customerPlan() {
        return "cust_potential_plan";
    }

    @RequestMapping("vip")
    @RequiresPermissions("customerPlan:vip")
    @PermissionName("正式客户跟进记录")
    public String customerFollow() {
        return "cust_vip_follow";
    }


    @RequestMapping("list")
    @ResponseBody
    public PageResult list(CustomerPlanQueryObject qo) {

        PageResult result = customerPlanService.query(qo);

        return result;
    }

    @RequestMapping("delete")
    @RequiresPermissions("customerPlan:delete")
    @PermissionName("删除记录/计划")
    @ResponseBody
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try{
            customerPlanService.deletePlan(id);
            result.setMessage("已将记录/计划删除");
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败");
        }

        return result;
    }

    @RequestMapping("save")
    @RequiresPermissions("customerPlan:save")
    @PermissionName("记录/计划保存")
    @ResponseBody
    public JsonResult save(CustomerPlan customerPlan) {
        JsonResult result = new JsonResult();
        try{
            customerPlanService.insert(customerPlan);
            result.setMessage("记录/计划保存成功");
        }catch (Exception e){
            e.printStackTrace();
            result.mark("保存失败");
        }

        return result;
    }

    @RequestMapping("edit")
    @RequiresPermissions("customerPlan:edit")
    @PermissionName("记录/计划编辑")
    @ResponseBody
    public JsonResult edit(CustomerPlan customerPlan) {
        JsonResult result = new JsonResult();
        try {
            customerPlanService.updateByPrimaryKey(customerPlan);
            result.setMessage("编辑记录/计划成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("编辑失败");
        }
        return result;
    }

    @RequestMapping("result")
    @RequiresPermissions("customerPlan:result")
    @PermissionName("标记执行结果")
    @ResponseBody
    public JsonResult result(Long id,Integer result) {
        JsonResult jsonResult = new JsonResult();
        try {
            customerPlanService.remark(id,result);
            jsonResult.setMessage("标记执行结果成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("标记失败");
        }
        return jsonResult;
    }
}